<template>
	<view class="modular">
		<view class="tips">请在90秒内快速回答以下问题</view>
		<view class="content">
			<view class="time">{{time}}s</view>
			<view class="questionModular">
				<view class="question" v-for="(item,index) in questionData" :key="index">
					<view class="title">
						<text>{{index < 9 ?'0'+Number(index+1) : index}}</text>
						<view>{{item.name}}</view>
					</view>
					<view class="option" v-for="(aitem,aindex) in item.adTopicsRepliesEntityList" :key="aindex">
						<view @click="answer(item,aitem)">
							<text>{{aitem.elections}}、{{aitem.value}}</text> 
							<image src="@/static/img/select@2x.png" v-if="aitem.checked == false"></image>  
							<image src="@/static/img/selected@2x.png" v-else></image>
						</view>
					</view>
				</view>
			</view>	
			<view class="sure" @click="sure">确认</view>
			
			<view class="transparent1"></view>
			<view class="transparent2"></view>
		</view>
		<u-modal v-model="showPopup" @confirm="previousPage" :content="showContent"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				questionData:{},
				time: 90,
				showPopup: false,
				showContent: '',
			}
		},
		onShow() {		
			this.question().then((response) => {
				if(response) {
					this.time = 90;
					this.countDown()
				}
			})
		},
		methods:{
			//选答案
			answer(item,aitem) {
				item.adTopicsRepliesEntityList.map((a) => {
					a.checked = false
					return a
				})
				aitem.checked = true;
			},
			//获取焦虑问卷
			question() {
				return new Promise((success,error)=>{
					this.$api.psychologyQuestion('ids=27').then((res)=>{
						if(res){
							this.questionData = res.data.result[27] && res.data.result[27].adTopicsRepliesEntityList.map((a)=>{
								a.adTopicsRepliesEntityList.map((b) => {
									b.checked = false;
									return b
								})
								return a
							})		
							success(res.data.result)
						}else{
							error('')
						}
					})
				})
			},
			countDown() {
				const _this = this
				_this.timer = setInterval(() => {
					if(_this.time <= 0) {
						_this.showPopup = true;
						_this.showContent = '未在规定时间内做题,点击确定后返回上一页';
						_this.timer && _this.clearTimer();
						return
						
						// location.reload() //刷新页面
						// return
					}else {
						_this.time--
					}
					
				},1000)
			},
			clearTimer() {//清除定时器
				clearInterval(this.timer);
				this.timer = null;
			},
			previousPage() {
				uni.navigateTo({
					url:'/pages/healthStatus/navigatorBar'
				})
			},
			sure() {
				const aa = this.questionData.map((a) => {
					const bresult = a.adTopicsRepliesEntityList.filter((b) => {
						if(b.checked){
							a.elections = b.elections
							a.setvalue = b.value
						}
						return b.checked
					})
					a.bresult = bresult
					return a
				})
				var aaArr = aa.filter((c,ci)=>{
					return c.bresult.length <= 0
				})
				
				if(aaArr.length > 0){
					uni.showToast({
						title:'请选择'+aaArr[0].name,
						icon:'none'
					})
					return
				}else {
					var outcome =  aa.filter((d) => {
						return d.bresult[0].elections != 'B'
					})
					this.clearTimer()
					if(outcome.length > 0) {
						uni.navigateTo({
							url:'/pages/healthStatus/depressed/create'
						})
						localStorage.setItem('rapidScreen',JSON.stringify(this.questionData))
					}else {
						uni.navigateTo({
							url:'/pages/healthStatus/depressed/state'
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/healthStatus/screening.scss';
</style>
